package com.google.sky.ai.simple;

/**
 * Creator class for {@link ScoredMove}. Caches most commonly used moves (without score).
 *
 * @author sslautin
 * @version 1.0 22.11.2014
 */
public class ScoredMoveFactory {

    private static final ScoredMove[][][] CACHE;

    static {
        CACHE = new ScoredMove[100][100][2];
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < 100; j++) {
                CACHE[i][j][0] = new ScoredMove(i, j, true);
                CACHE[i][j][1] = new ScoredMove(i, j, false);
            }
        }
    }

    public static ScoredMove createScoredMove(
            final int x,
            final int y,
            final boolean mine,
            final int score
    ) {

        ScoredMove move = createScoredMove(x, y, mine);
        move.setScore(score);
        return move;
    }

    public static ScoredMove createScoredMove(
        final int x,
        final int y,
        final boolean mine
    ) {

        if (x >= 0 && y >= 0 && x < 100 && y < 100) {
            if (mine) {
                return CACHE[x][y][0];
            } else {
                return CACHE[x][y][1];
            }
        } else {
            return new ScoredMove(x, y, mine);
        }
    }
}
